﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using StudentEnrollment.Api.DTOs.Course;
using StudentEnrollment.Api.DTOs.Enrollment;
using StudentEnrollment.Data;
using StudentEnrollment.Data.Contracts;

namespace StudentEnrollment.Api.Endpoints
{
    public static class EnrollmentEndpoints
    {
        public static void MapEnrollmentEndpoints(this IEndpointRouteBuilder router)
        {
            router.MapGet("/api/Enrollment", async (IEnrollmentRepository repo, IMapper mapper) =>
            {
                var enrollments = await repo.GetAllAsync();
                var data = mapper.Map<List<EnrollmentDto>>(enrollments);
                return data;
            })
                .WithTags(nameof(Enrollment))
                .WithName("GetAllEnrollment")
                .Produces<List<EnrollmentDto>>(StatusCodes.Status200OK);



            router.MapGet("/api/Enrollment/{id}", async (int id, IEnrollmentRepository repo, IMapper mapper) =>
            {
                return await repo.GetAsync(id) is Enrollment model ? Results.Ok(mapper.Map<EnrollmentDto>(model))

                    : Results.NotFound();
            })
                .WithTags(nameof(Enrollment))
                .WithName("GetEnrollmentById")
                .Produces<EnrollmentDto>(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound);



            router.MapPut("/api/Enrollment/{id}", async (int Id, EnrollmentDto enrollmentDto, IEnrollmentRepository repo, IMapper mapper) =>
            {
                var foundModel = await repo.GetAsync(Id);
                if (foundModel is null)
                {
                    return Results.NotFound();
                }
                mapper.Map(enrollmentDto, foundModel);

                // update model properties here 
                await repo.UpdateAsync(foundModel);

                return Results.NoContent();
            })
                .WithTags(nameof(Enrollment))
                .WithName("UpdateEnrollment")
                .Produces(StatusCodes.Status404NotFound)
                .Produces(StatusCodes.Status204NoContent);


            router.MapPost("/api/Enrollment", async (CreateEnrollmentDto createEnrollmentDto, IEnrollmentRepository repo, IMapper mapper) =>
            {
                var enrollment = mapper.Map<Enrollment>(createEnrollmentDto);

                await repo.AddAsync(enrollment);
                return Results.Created($"/Enrollments/{enrollment.Id}", enrollment);
            })
                .WithTags(nameof(Enrollment))
                .WithName("CreateEnrollment")
                .Produces<Enrollment>(StatusCodes.Status201Created);




            router.MapDelete("/api/Enrollment/{id}", async (int Id, IEnrollmentRepository repo) =>
            {
               return await repo.DeleteAsync(Id) ? Results.NoContent() : Results.NotFound();                
            })
                .WithTags(nameof(Enrollment))
                .WithName("DeleteEnrollment")
                .Produces<Enrollment>(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound);

        }

    }
}
